﻿/* Copyright (C) 2010 - 2011 by John Cronin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
    partial class Assembler
    {
        public class LockedInt
        {
            int _count = 0;
            public int Count { get { lock (this) { return _count; } } set { lock (this) { _count = value; } } }
            static public implicit operator int(LockedInt rtc) { return rtc.Count; }
            static public implicit operator LockedInt(int val) { LockedInt ret = new LockedInt(); ret.Count = val; return ret; }
            static public LockedInt operator ++(LockedInt rtc) { lock (rtc) { rtc._count++; return rtc; } }
            static public LockedInt operator --(LockedInt rtc) { lock (rtc) { rtc._count--; return rtc; } }
            public int Increment { get { lock (this) { return _count++; } } }
            public override string ToString() { lock (this) { return _count.ToString(); } }
        }

#if MT
        internal class TTCRequest { public TypeToCompile ttc; public IOutputFile of; }
        internal class MTCRequest { public MethodToCompile mtc; public IOutputFile of; }
        internal class FTCRequest { public FieldToCompile ftc; public IOutputFile of; }
#endif

        LockedInt rtc = new LockedInt();        /* number of threads running */

        public void DoCompile(IOutputFile w) { DoCompile(w, null, null); }
        public void DoCompile(IOutputFile w, IDoCompileFeedback dcf) { DoCompile(w, null, dcf); }
        public void DoCompile(IOutputFile w, List<string> unimplemented_internal_calls, IDoCompileFeedback dcf)
        {
            while (Requestor.MoreToDo || (rtc.Count > 0))
            {
                while (Requestor.MoreMethods)
                {
                    MethodToCompile? mtc = null;

                    lock (Requestor.meth_lock)
                    {
                        if (Requestor.MoreMethods)
                            mtc = Requestor.GetNextMethod();
                    }

                    if (mtc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleMethodFeedback(mtc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleMethod).Start(new MTCRequest { mtc = mtc.Value, of = w });
#else
                        AssembleMethod(mtc.Value, w, unimplemented_internal_calls);
#endif
                    }
                }

                while (Requestor.MoreTypes)
                {
                    TypeToCompile? ttc = null;

                    lock (Requestor.type_lock)
                    {
                        if (Requestor.MoreTypes)
                            ttc = Requestor.GetNextType();
                    }

                    if (ttc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleTypeFeedback(ttc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleType).Start(new TTCRequest { ttc = ttc.Value, of = w });
#else
                        AssembleType(ttc.Value, w);
#endif
                    }
                }

                while (Requestor.MoreTypeInfos)
                {
                    TypeToCompile? ttc = null;

                    lock (Requestor.ti_lock)
                    {
                        if (Requestor.MoreTypeInfos)
                            ttc = Requestor.GetNextTypeInfo();
                    }

                    if (ttc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleTypeInfoFeedback(ttc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleTypeInfo).Start(new TTCRequest { ttc = ttc.Value, of = w });
#else
                        AssembleTypeInfo(ttc.Value, w);
#endif
                    }
                }

                while (Requestor.MoreFieldInfos)
                {
                    FieldToCompile? ftc = null;

                    lock (Requestor.fi_lock)
                    {
                        if (Requestor.MoreFieldInfos)
                            ftc = Requestor.GetNextFieldInfo();
                    }

                    if (ftc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleFieldInfoFeedback(ftc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleFieldInfo).Start(new FTCRequest { ftc = ftc.Value, of = w });
#else
                        AssembleFieldInfo(ftc.Value, w);
#endif
                    }
                }

                while (Requestor.MoreMethodInfos)
                {
                    MethodToCompile? mtc = null;

                    lock (Requestor.mi_lock)
                    {
                        if (Requestor.MoreMethodInfos)
                            mtc = Requestor.GetNextMethodInfo();
                    }

                    if (mtc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleMethodInfoFeedback(mtc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleMethodInfo).Start(new MTCRequest { mtc = mtc.Value, of = w });
#else
                        AssembleMethodInfo(mtc.Value, w);
#endif
                    }
                }
            }
        }

        public interface IDoCompileFeedback
        {
            void AssembleMethodFeedback(Assembler.MethodToCompile mtc);
            void AssembleTypeFeedback(Assembler.TypeToCompile ttc);
            void AssembleTypeInfoFeedback(Assembler.TypeToCompile ttc);
            void AssembleFieldInfoFeedback(Assembler.FieldToCompile ftc);
            void AssembleMethodInfoFeedback(Assembler.MethodToCompile mtc);
        }
    }
}
